package com.pine.sunshineelder.waiter.service.impl;

import static com.pine.sunshineelder.common.validator.ValidateHelper.$;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderDoctorDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderBedEntity;
import com.pine.sunshineelder.common.entity.ElderDoctorEntity;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.waiter.ao.ElderDoctorAo;
import com.pine.sunshineelder.waiter.service.ElderDoctorService;
import com.pine.sunshineelder.waiter.vo.ElderDoctorVo;

@Service
public class ElderDoctorServiceImpl implements ElderDoctorService, StringHelper {
	@Autowired
	ElderDoctorDao elderDoctorDao;

	@Override
	public ElderDoctorVo addElderDoctor(ElderDoctorAo ao) throws Exception {

		// 验证参数
		// 非空
		String elderId = $("老人id", ao.getElderId());
		String docotorId = $("护工id", ao.getDoctorId());
		String createUserId = $("创建人id", ao.getCreateUserId());
		String reason = $("原因", ao.getReason());

		// 执行业务逻辑

		ElderDoctorEntity u = new ElderDoctorEntity();
		u.setId(uuid());
		u.setElderId(ao.getElderId());
		u.setDoctorId(ao.getDoctorId());
		u.setCreateUserId(ao.getCreateUserId());
		u.setCreateTime(ao.getCreateTime());
		u.setEndTime(ao.getEndTime());
		u.setReason(ao.getReason());

		elderDoctorDao.insert(u);
		;

		// 组装业务逻辑返回
		ElderDoctorVo dto = new ElderDoctorVo();
		dto.setId(u.getId());
		dto.setElderId(u.getElderId());
		dto.setDoctorId(u.getDoctorId());
		dto.setCreateUserId(u.getCreateUserId());
		dto.setCreateTime(toString(u.getCreateTime()));
		dto.setEndTime(toString(u.getEndTime()));
		dto.setReason(u.getReason());

		return dto;
	}

	public static String toString(LocalDateTime time) {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
		return time.format(dtf);
	}

	public static String toString(LocalDate date) {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return date.format(dtf);
	}

	@Override
	public ElderDoctorVo selectById(String id) throws Exception {
		String id1 = ValidateHelper.$("id", id);
		// 执行业务逻辑

		ElderDoctorEntity u1;
		u1 = elderDoctorDao.selectById(id1);

		ElderDoctorVo u = new ElderDoctorVo();
		u.setId(u1.getId());
		u.setElderId(u1.getElderId());
		u.setDoctorId(u1.getDoctorId());
		u.setCreateUserId(u1.getCreateUserId());
		u.setCreateTime(toString(u1.getCreateTime()));
		u.setEndTime(toString(u1.getEndTime()));
		u.setReason(u1.getReason());
		return u;
	}

	@Override
	public void deleteElderDoctorById(String id) throws Exception {

		// 验证清理参数
		// 非空
		String id1 = ValidateHelper.$("id", id);

		// 执行业务逻辑

		elderDoctorDao.delete(id1);

	}

	@Override
	public QueryResult queryByKey(String key, Integer pageNo, Integer pageSize) throws Exception {
		key = trimAsNull(key);
		if (key != null) {
			key = "%" + key + "%";
		}

		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 6;
		}
		List<ElderDoctorEntity> rows = elderDoctorDao.selectByKey(key, pageNo, pageSize);

		PageInfo pageInfo = new PageInfo(rows);

		int totalRows = (int) pageInfo.getTotal();

		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows(totalRows);
		result.setRows(rows);

		return result;

	}

	
	@Override
	public ElderDoctorEntity modifyElderBed(String id, Map<String, String> ao) throws Exception {

		return null;
	}

}
